package cn.edu.laud.sany.utils;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.cassandra.thrift.Mutation;

public class MutationMap
{
	private Map<ByteBuffer, Map<String, List<Mutation>>> mutation_mapMap;
	
	public int mutationCount;
	
	public MutationMap()
	{
		mutation_mapMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
		mutationCount = 0;
	}
	
	public Map<ByteBuffer, Map<String, List<Mutation>>> getMap()
	{
		return mutation_mapMap;
	}
	
	public void clearMutation()
	{
		mutation_mapMap.clear();
		mutationCount = 0;
	}
	
	public synchronized void addMutation(ByteBuffer keyBuffer, String cfName, Mutation mutation)
	{
		Map<String, List<Mutation>> cfMap;
		if (mutation_mapMap.containsKey(keyBuffer))
		{
			cfMap = mutation_mapMap.get(keyBuffer);
		}
		else
		{
			cfMap = new HashMap<String, List<Mutation>>();
			mutation_mapMap.put(keyBuffer, cfMap);
		}
		List<Mutation> mutations;
		if (cfMap.containsKey(cfName))
		{
			mutations = cfMap.get(cfName);
		}
		else
		{
			mutations = new ArrayList<Mutation>();
			cfMap.put(cfName, mutations);
		}
		mutations.add(mutation);
		mutationCount++;
	}
}
